package util;

import context.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.awt.geom.Arc2D;

import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;

public class Util implements Context.Constants
{ //private static final boolean DEBUG=true; // Overriding Context.Constants.DEBUG


  // Locally used colors
  private static final Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
//  private static final Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

//***************************************************************************
//*** UTILITY
//***************************************************************************
public static Appearance colorAppearance(Color3f color)
{ Appearance app=new Appearance();
  app.setColoringAttributes(new ColoringAttributes(color,ColoringAttributes.SHADE_GOURAUD));
  app.setMaterial(new Material(color,black,color,black,80.0f));
  return(app);
}

//***************************************************************************
//*** UTILITY
//***************************************************************************
public static Appearance transparentAppearance(Color3f color)
{ Appearance app=new Appearance();
  // Set up the transparency properties
  TransparencyAttributes ta = new TransparencyAttributes();
  ta.setTransparencyMode(TransparencyAttributes.BLENDED);
  ta.setTransparency(0.6f);
  app.setTransparencyAttributes(ta);
  // Set up the polygon attributes
  PolygonAttributes pa = new PolygonAttributes();
  pa.setCullFace(PolygonAttributes.CULL_NONE);
  app.setPolygonAttributes(pa);
  // Set up the material properties
  app.setMaterial(new Material(color,black,color,black,1.0f));
  return(app);
}

  // ******************************************************************
  // *** UTILITIES: NYE !!!!
  // ******************************************************************



  public static void draw(Graphics2D g,Shape s)
  { g.draw(s); }

  public static void fill(Graphics2D g,Shape s,Color c)
  { if(c!=null)
    { Color color=g.getColor();
      g.setColor(c);
      g.fill(s);
      g.setColor(color);
    }
  }

  public static void fillAndDraw(Graphics2D g,Shape s,Color c)
  { fill(g,s,c); g.draw(s); }

  public static void drawLine(Graphics2D g,Point2D.Double p1,Point2D.Double p2)
  { drawLine(g,p1.x,p1.y,p2.x,p2.y); }

  public static void drawLine(Graphics2D g,double x1,double y1,double x2,double y2)
  { g.draw(new Line2D.Double(x1,y1,x2,y2)); }

  public static void drawRect(Graphics2D g,double x,double y,double width,double height)
  { draw(g,new Rectangle2D.Double(x,y,width,height)); }

  public static void fillRect(Graphics2D g,double x,double y,double width,double height,Color c)
  { fill(g,new Rectangle2D.Double(x,y,width,height),c); }

  public static void drawArc(Graphics2D g,double x,double y,double width,double height,double startAngle,double arcAngle)
  { g.draw(new Arc2D.Double(x,y,width,height,startAngle,arcAngle,Arc2D.OPEN)); }

  public static void drawString(Graphics2D g,String str,double x,double y)
  { g.drawString(str,(float)x,(float)y); }

  public static void label(Graphics2D g,String str,double x,double y)
  { Font font=g.getFont();
    Rectangle2D box=font.getStringBounds(str,new java.awt.font.FontRenderContext(null,false,false));
    double width=box.getWidth()+8;
    double height=box.getHeight();
    fillRect(g,x-width/2,y-height/2,width,height,Color.white);
    drawString((Graphics2D)g,str,x-width/2+4,y+height/2-4);
  }

}
